﻿using CPPEI;
using CPPEI.WebApi;
using SLG.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Ser = SLG.Domain.Services;
using Ent = SLG.Domain.Entities;
using V = SLG.Domain.Views;
using D = SLG.Domain.Datas;

namespace SLG.WebApi.City
{
    /// <summary>
    /// 获得城市
    /// </summary>
    [AuthGetAction("City", "Get", "1.0")]
    public class GetAction : BaseAction<GetParams, GetResult>
    {
        protected override ActionResult<GetResult> OnExecute(ActionContext context, SysParameters sysParams, GetParams opParams)
        {
            int userId = Convert.ToInt32(context.UserId);

            Ser.QueryService service = new Ser.QueryService();
            SingleResult<D.CityAndResouce> serResult = service.QueryCityAndResource(userId, opParams.CityId);

            if (serResult.IsSuccess)
            {
                GetResult actResult = new GetResult();

                actResult.Id = serResult.Data.City.Id;
                actResult.LordId = serResult.Data.City.LordId;
                actResult.Name = serResult.Data.City.Name;
                actResult.Area = serResult.Data.City.Area;
                actResult.PosX = serResult.Data.City.PosX;
                actResult.PosY = serResult.Data.City.PosY;
                actResult.Level = serResult.Data.City.Level;
                actResult.Experience = serResult.Data.City.Experience;
                actResult.CreatedTime = serResult.Data.City.CreatedTime;
                actResult.IsCapital = serResult.Data.City.IsCapital;

                for (int index = 0; index < serResult.Data.Resources.Count; index++)
                {
                    GetResult.Resource resource = new GetResult.Resource();

                    resource.Id = serResult.Data.Resources[index].Id;
                    resource.Value = Convert.ToInt32(serResult.Data.Resources[index].Value);
                    resource.Rate = Convert.ToInt32(serResult.Data.Resources[index].Rate);
                    actResult.Resources.Add(resource);
                }
                return new ActionResult<GetResult>(actResult);
            }
            else
            {
                return new ActionResult<GetResult>(serResult);
            }
        }
    }

    public class GetParams
    {
        public Guid CityId { get; set; }
    }

    public class GetResult
    {
        public Guid Id { get; set; }

        public Guid LordId { get; set; }

        public string Name { get; set; }

        public int Area { get; set; }

        public int PosX { get; set; }

        public int PosY { get; set; }

        public int Level { get; set; }

        /// <summary>
        /// 经验
        /// </summary>
        public int Experience { get; set; }

        public DateTime CreatedTime { get; set; }

        public bool IsCapital { get; set; }

        public List<Resource> Resources = new List<Resource>();

        public class Resource
        {
            public int Id { get; set; }

            public int Value { get; set; }

            public int Rate { get; set; }
        }
    }
}
